using System;
using System.Collections;
using System.Collections.Generic;
using Me.Prettyprint.Hector.Api.Ddl;
using Org.Apache.Cassandra.Thrift;

namespace Me.Prettyprint.Cassandra.Service
{
	
	public class ThriftKsDef : KeyspaceDefinition
	{
		private const string REPLICATION_FACTOR = "replication_factor";
		public const string DEF_STRATEGY_CLASS = "org.apache.cassandra.locator.SimpleStrategy";
		public const string NETWORK_TOPOLOGY_STRATEGY = "org.apache.cassandra.locator.NetworkTopologyStrategy";
        #region Constructors
        protected ThriftKsDef()
        {
            StrategyOptions = new Dictionary<string, string>();
            StrategyOptions[REPLICATION_FACTOR] = "1";
            CfDefs = new List<ColumnFamilyDefinition>();
            DurableWrites = true;
        }
        public ThriftKsDef(KsDef k) : this()
        {
            Name = k.Name;
            StrategyClass = k.Strategy_class;
            StrategyOptions = k.Strategy_options != null ? k.Strategy_options : new Dictionary<string, string>();
            DurableWrites = k.Durable_writes;
            CfDefs = ThriftCfDef.FromThriftList(k.Cf_defs);
        }
        public ThriftKsDef(string keyspaceName) : this()
        {
            Name = keyspaceName;
            StrategyClass = DEF_STRATEGY_CLASS;
            ReplicationFactor = 1;
        }

        public ThriftKsDef(KeyspaceDefinition keyspaceDefinition) : this()
        {
            Name = keyspaceDefinition.Name;
            StrategyClass = keyspaceDefinition.StrategyClass;
            StrategyOptions = keyspaceDefinition.StrategyOptions;
            ReplicationFactor = keyspaceDefinition.ReplicationFactor;
            CfDefs = keyspaceDefinition.CfDefs;
        }
        public ThriftKsDef(string keyspaceName, string strategyClass, 
            int replicationFactor, List<ColumnFamilyDefinition> cfDefs) : this()
        {
            Name = keyspaceName;
            StrategyClass = strategyClass;
            ReplicationFactor = replicationFactor;
            CfDefs = cfDefs;
        }
        #endregion
        public string Name
        {
            get;protected set;
        }
        public string StrategyClass
        {
            get;protected set;
        }
        public Dictionary<string,string> StrategyOptions
        {
            get;set;
        }
        public bool DurableWrites
        {
            get;set;
        }
        public List<ColumnFamilyDefinition> CfDefs
        {
            get;set;
        }
		public int ReplicationFactor
		{
            get
            {
                return int.Parse(StrategyOptions[REPLICATION_FACTOR]);
            }
            set
            {
                StrategyOptions[REPLICATION_FACTOR] = value.ToString();
            }
		}
		
        public static List<KeyspaceDefinition> FromThriftList(List<KsDef> ks)
        {
            if (ks == null || ks.Count == 0)
            {
                return new List<KeyspaceDefinition>();
            }
            List<KeyspaceDefinition> l = new List<KeyspaceDefinition>(ks.Count);
            foreach (KsDef k in ks)
            {
                l.Add(new ThriftKsDef(k));
            }
            return l;
        }
		
		public virtual void AddCfDef(CfDef cfDef)
		{
			CfDefs.Add(new ThriftCfDef(cfDef));
		}
		
		public virtual void AddColumnFamilyDefinition(ColumnFamilyDefinition columnFamilyDefinition)
		{
			CfDefs.Add(new ThriftCfDef(columnFamilyDefinition));
		}
		public virtual KsDef ToThrift()
		{
			KsDef def = new Org.Apache.Cassandra.Thrift.KsDef();
            def.Name = Name;
            def.Strategy_class = StrategyClass;
            def.Cf_defs = ThriftCfDef.ToThriftList(CfDefs);
			def.Strategy_options = StrategyOptions;
			def.Durable_writes = DurableWrites;
			return def;
		}
		public override string ToString()
		{
            return typeof(ThriftKsDef).ToString();
		}
	}
}